package com.team233.zuccecho.service.impl;

import com.team233.zuccecho.Bean.Clazz;
import com.team233.zuccecho.Bean.Course;
import com.team233.zuccecho.Bean.Student;
import com.team233.zuccecho.Bean.Teacher;
import com.team233.zuccecho.dto.ClazzDto;
import com.team233.zuccecho.dto.CourseDto;
import com.team233.zuccecho.dto.StudentDto;
import com.team233.zuccecho.exception.EchoServiceException;
import com.team233.zuccecho.repository.ClazzRepository;
import com.team233.zuccecho.repository.CourseRepository;
import com.team233.zuccecho.repository.StudentRepository;
import com.team233.zuccecho.repository.TeacherRepository;
import com.team233.zuccecho.service.inter.CourseInterface;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CourseImpl implements CourseInterface {
    @Autowired
    private CourseRepository courseRepository;
    @Autowired
    private ClazzRepository clazzRepository;
    @Autowired
    private TeacherRepository teacherRepository;
    @Autowired
    private StudentRepository studentRepository;

    /**
     * {
     *     "name":"SpringBoot",
     *     "type":"bixiu",
     *     "clazzList":[
     *         {
     *             "clazz_name":"SpringBoot clazz1",
     *             "clazz_address":"4-201",
     *             "clazz_time":"zhouyi 15:00"
     *         },{
     *             "clazz_name":"SpringBoot clazz2",
     *             "clazz_address":"4-204",
     *             "clazz_time":"zhousan 15:00"
     *         }
     *     ]
     * }
     */
    @Override
    public Long addCourse(CourseDto courseDto) throws EchoServiceException {
        if(courseDto.getId()!=null)
            throw new EchoServiceException("新建课程不能上传id");
        if(courseRepository.findByName(courseDto.getName())!=null)
            throw new EchoServiceException("该课程已存在");

        Course course=new Course();
        BeanUtils.copyProperties(courseDto,course);
        if(courseDto.getClazzList()!=null){
            for(ClazzDto clazzDto:courseDto.getClazzList()){
                if(clazzRepository.findByName(clazzDto.getClazz_name())==null){
                    Clazz clazz=new Clazz();
                    BeanUtils.copyProperties(clazzDto,clazz);
                    clazz.setCourse(course);
                    course.getClazzList().add(clazz);
                }
            }
        }
        courseRepository.save(course);
        return course.getId();
    }

    @Override
    public Integer deleteCourse(List<CourseDto> courseDtoList) throws EchoServiceException {
        for(CourseDto courseDto:courseDtoList){
            Course course = courseRepository.findByName(courseDto.getName());
            if(course!=null) {
                System.out.println(course.getClazzList().size());
                if(course.getClazzList().size()==0){
                    courseRepository.delete(course);
                    return 1;
                }
            }
        }
        throw new EchoServiceException("删除失败");
    }

    @Override
    public Integer addClazz(List<ClazzDto> clazzDtoList) throws EchoServiceException {
        List<Clazz> clazzList=new ArrayList<>();
        for(ClazzDto clazzDto:clazzDtoList){
            if(clazzRepository.findByName(clazzDto.getClazz_name())==null){
                Clazz clazz=new Clazz();
                BeanUtils.copyProperties(clazzDto,clazz);
                clazzList.add(clazz);
            }
        }

        if(clazzList.size()==0) throw new EchoServiceException("添加失败");
        else {
            clazzRepository.saveAll(clazzList);
            return 1;
        }
    }

    @Override
    public Integer deleteClazz(List<ClazzDto> clazzDtoList) throws EchoServiceException {
        List<Clazz> clazzList=new ArrayList<>();
        for(ClazzDto clazzDto:clazzDtoList){
            Clazz clazz=clazzRepository.findByName(clazzDto.getClazz_name());
            if(clazz!=null){
                clazzList.add(clazz);
                clazzRepository.delete(clazz);
            }
        }
        if (clazzList.size()==0) throw new EchoServiceException("删除失败");
        return 1;
    }

    @Override
    public Integer AddTea(List<ClazzDto> clazzDtoList) throws EchoServiceException {
        for(ClazzDto clazzDto:clazzDtoList){
            Clazz clazz=clazzRepository.findByName(clazzDto.getClazz_name());
            if(clazz!=null){
                Teacher teacher=teacherRepository.findTeaById(clazzDto.getTeacher_id());
                if(teacher!=null){
                    clazz.setTeacher(teacher);
                    clazzRepository.save(clazz);
                }
            }
        }
        return 1;
    }

    @Override
    public Integer AddStu(List<ClazzDto> clazzDtoList) throws EchoServiceException {
        for(ClazzDto clazzDto:clazzDtoList){
            Clazz clazz=clazzRepository.findByName(clazzDto.getClazz_name());
            if(clazz!=null){
                for(StudentDto stu:clazzDto.getStudentDtoList())
                {
                    Student student=studentRepository.findStuById(stu.getStu_id());
                    if(student!=null){
                        clazz.getStudentlist().add(student);
                    }
                }
                clazzRepository.save(clazz);
            }
        }
        return 1;
    }

    @Override
    public Integer RemoveStu(List<ClazzDto> clazzDtoList) throws EchoServiceException {
        for(ClazzDto clazzDto:clazzDtoList){
            Clazz clazz=clazzRepository.findByName(clazzDto.getClazz_name());
            if(clazz!=null){
                for(StudentDto stu:clazzDto.getStudentDtoList())
                {
                    Student student=studentRepository.findStuById(stu.getStu_id());
                    if(student!=null){
                        clazz.getStudentlist().remove(student);
                    }
                }
                clazzRepository.save(clazz);
            }
        }
        return 1;
    }
}
